package com.itheima.edu.info.manager.dao.impl;

import com.itheima.edu.info.manager.dao.BaseTeacherDao;
import com.itheima.edu.info.manager.domain.Teacher;

import java.util.ArrayList;

public class OtherTeacherDao implements BaseTeacherDao {
    private static ArrayList<Teacher> teachers = new ArrayList<>();
    static {
        Teacher teacher = new Teacher("heima001","张三","30岁","1980-11-11");
        Teacher teacher1 = new Teacher("heima002","李斯","29岁","1981-11-11");
        teachers.add(teacher);
        teachers.add(teacher1);
        System.out.println("----------teacher集合初始化完成----------");
    }
    public boolean addTeacher(Teacher teacher) {
        teachers.add(teacher);
        return true;
    }

    public Teacher[] findAllTeacher() {
        Teacher[] teas = new Teacher[teachers.size()];
        for (int i = 0; i < teas.length; i++) {
            teas[i] = teachers.get(i);
        }
        return teas;
    }

    public Teacher selectTeacher(String id) {
        for (int i = 0; i < teachers.size(); i++) {
            if (teachers.get(i).getId().equals(id))
                return teachers.get(i);
        }
        return null;
    }

    public Teacher updataTeacher(Teacher teacher) {
        for (int i = 0; i < teachers.size(); i++) {
            if (teachers.get(i).getId().equals(teacher.getId())){
                teachers.get(i).setName(teacher.getName());
                teachers.get(i).setAge(teacher.getAge());
                teachers.get(i).setBirthday(teacher.getBirthday());
                return teachers.get(i);
            }
        }
        return null;
    }

    public boolean delectTeacher(String id) {
        //遍历老师数组
        for (int i = 0; i < teachers.size(); i++) {
            if (teachers.get(i).getId().equals(id)) {
                teachers.remove(i);
                return true;
            }
        }
        //不存在返回false
        return false;
    }
}
